﻿using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.EntityFrameworkCore;
using ERP.Workflow.Designer.Domain;

namespace ERP.Workflow.Designer.EntityFrameworkCore
{
 
    public class WorkflowDesignerDbContextFactory : IDesignTimeDbContextFactory<WorkflowDesignerDbContext>
    {
        public WorkflowDesignerDbContext CreateDbContext(string[] args)
{
            WorkflowDesignerEfCoreEntityExtensionMappings.Configure();

            var configuration = BuildConfiguration();
            string url = configuration.GetConnectionString(WorkflowDesignerDbProperties.ConnectionStringName) ?? configuration.GetConnectionString("Default");
            var builder = new DbContextOptionsBuilder<WorkflowDesignerDbContext>()
                .UseMySql(url, MySqlServerVersion.LatestSupportedServerVersion);

            return new WorkflowDesignerDbContext(builder.Options);
        }

        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                //.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../ERP.Workflow.Designer.DbMigrator/"))
                .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../../../src/ERP.DbMigrator/"))
                .AddJsonFile("appsettings.json", optional: false);

            return builder.Build();
        }
    }

}
